		estimates clear
	
	set scheme s2color  

		* All Attacks
	
		loc dir "/Users/mateo/Desktop/"
	
	cd "/Users/mateo/Desktop/"
	
		use "/Users/mateo/Desktop/master_violence_landtitle_caracteristicas.dta", clear
		
		
		loc controls  rainfall coca_0_1 presence_mines1560 num_encomiendas1560 royalroaddist distancia_mercado pct_minority85 conflictos_1901_1917 conflictos_1918_1931 Violencia_48_a_53 anucraids1971_78 pop95  spatlagguerrsum100_100_1995 ltotal_plots_rfmd_1960_85 totviolencia85
	  
		
		foreach x in 1995 {
		
			preserve
			
				keep if Año<=`x'
							
				collapse (mean)  spatlagguerrsum100_100, by(codmpio)
				
				ren (spatlagguerrsum100_100)=_`x'
				
				tempfile neighbor
				save `neighbor', replace
			
			
			restore
			
			merge m:1 codmpio using `neighbor'
			drop _m
		
		
		}
		
		** Controls X year
			loc controlsXyear
			loc x
			
			foreach control of loc controls {
			
				loc x c.`control'##c.Año
				
				loc controlsXyear `controlsXyear' `x' 

			}
			
	** Main Analysis Sample
			
		by codmpio (Año), sort: gen first_title = sum(landtitle) == 1
		gen first_title_year=Año if first_title==1
		
		bys codmpio (first_title_year): replace first_title_year= first_title_year[_n-1] if mi(first_title_year) 
		
		gen chyears=Año - first_title_year
		replace chyears=-10 if chyears<=-10
		replace chyears=10 if chyears>=10
		
		gen title_adopted=0 if first_title_year > Año
		replace title_adopted = 1 if first_title_year<=Año
		replace title_adopted =. if mi(first_title_year)
		egen all_attacks=rowtotal(sumattacks_public sumattacks_guerrilla sumattacks_paramilitary)
		
		lab var sumattacks_public 			"Police & Army attacks"
		lab var sumattacks_paramilitary 	"Paramilitary attacks"
		lab var sumattacks_guerrilla 		"Guerrilla attacks"
	
	
	drop chyears title_adopted

		by codmpio (Año), sort: gen first_title_pl = sum(landtitle_placebo) == 1
		gen first_title_pl_year=Año if first_title_pl==1
		
		bys codmpio (first_title_pl_year): replace first_title_pl_year= first_title_pl_year[_n-1] if mi(first_title_pl_year) 
		
		gen chyears=Año - first_title_pl_year
		replace chyears=-10 if chyears<=-10
		replace chyears=10 if chyears>=10
		
		gen title_adopted=0 if first_title_pl_year > Año
		replace title_adopted = 1 if first_title_pl_year<=Año
		replace title_adopted =. if mi(first_title_pl_year)		
		
		foreach var of varlist sumattacks_public sumattacks_paramilitary sumattacks_guerrilla {
		
			preserve
			
				gen as_`var'=asinh(`var')	
				loc varlab: var lab `var'
				
				for any low med high: gen X=.
				qui xi : areg  as_`var'  `controlsXyear' Año##coddepto i.chyears, r cluster(codmpio) absorb(codmpio)
				for Y in num 2/10 11/20: replace med=_b[_Ichyears_Y] if _Ichyears_Y==1 
				for Y in num 2/10 11/20: replace low=_b[_Ichyears_Y]-1.64*_se[_Ichyears_Y] if _Ichyears_Y==1 		//Note:10% C.I.
				for Y in num 2/10 11/20: replace high=_b[_Ichyears_Y]+1.64*_se[_Ichyears_Y] if _Ichyears_Y==1 //Note:10% C.I.
				replace med=0 if chyears==0 
				replace high=0+1*(_se[_Ichyears_10]+_se[_Ichyears_11])/2 if chyears==0 
				replace low=0-1*(_se[_Ichyears_10]+_se[_Ichyears_11])/2 if chyears==0 
				for Z in var low med high: replace Z=Z-(_b[_Ichyears_2]+_b[_Ichyears_3]+_b[_Ichyears_4]+_b[_Ichyears_5]+_b[_Ichyears_6] + _b[_Ichyears_7] + _b[_Ichyears_8] + _b[_Ichyears_9] + _b[_Ichyears_10] +0)/10 
				for Z in var low med high: replace Z=0 if chyears==0 
				twoway (line high chyears, sort lpattern(dash) color(red)) (line low chyears, sort lpattern(dash) color(red)) (connected med chyears, color(black) sort), ytitle("Point Estimate") ylabel(-0.6 "-60%" -0.4 "-40%" -0.2 "-20%"  0 "0%"  .2 "20%" .4 "40%" .6 "60%" .8 "80%"   1 "100%" 1.2 "120%", angle(horizontal)) yline(0, lcolor(black)) xtitle(Years Since/Until First Land Title Assigned) xlabel(-10(2)10 -10 "<-10 yrs" 10 ">10 yrs") xline(0, lcolor(black)) /*title(`"{bf: `varlab'}"', size(vlarge))*/ legend(order(2 "Coefficient Estimates" 1 "10% C.I.") off) xsize(5) ysize(3.5) name(`var', replace) graphregion(color(white)) 
				graph export `"as_`var'_placebo.png"', replace
				
			restore
				
		}
			
		graph combine sumattacks_public sumattacks_paramilitary sumattacks_guerrilla , ///
		saving(`"violence_phasein_placebo.png"', replace) ycommon cols(2)  graphregion(color(white))  altshrink
			
		graph export `"violence_phasein_placebo.png"', replace
	
